﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Web;
using DTO;
using Moved;

namespace BUS
{
    public class CheckBUS
    {
         private static String _connString = "";
        
        static CheckBUS()
        {
            _connString = ConfigurationManager.ConnectionStrings["WebChamCongConnectionString"].ConnectionString.ToString(); 
        }
        public static List<CheckDTO> GetJobByID(Int32 userId)
        {
            Int32 localID = userId;

            List<CheckDTO> listNhanViens = new List<CheckDTO>();

            using (Moved.Moved_DB dc = new Moved_DB(_connString))
            {
                var nhanViens = (from nv in dc.AL_CheckInOut
                                 join user in dc.AL_UserInfo on nv.UserID equals user.UserID
                                 where nv.UserID == localID
                                 select new
                                 {
                                     id = nv.UserID,
                                     Fullname=user.FullName,
                                     partmentid=user.DepartmentID,
                                     machine=nv.MachineID,
                                     checktype=nv.CheckType,
                                     verify=nv.VerifyMode,
                                     workcode=nv.WorkCode
                                 }
                                         ).Distinct().OrderBy(temp => temp.id);

                foreach (var item in nhanViens)
                {
                    CheckDTO nv = new CheckDTO();
                    nv.UserID = item.id;
                    nv.MachineID = (int)item.machine;
                    nv.ChecType = (int)item.checktype;

                    nv.VerifyMode = (int)item.verify;
                    nv.PartmentID = (int)item.partmentid;
                    nv.WorkCode = (int)item.workcode;
                    nv.FullName = item.Fullname;
                    listNhanViens.Add(nv);
                
                }

            }
            return listNhanViens;
        }
     
        public static int demCheckin(int Ngay, int thang, int nam, Int32 localID)
        {
            int Earlytime = 0;
            using (Moved.Moved_DB dc = new Moved_DB(_connString))
            {
                int tre = (from nv in dc.AL_CheckInOut
                                   join user in dc.AL_UserInfo on nv.UserID equals user.UserID
                                   where nv.CheckTime.Month == thang
                                         && nv.CheckTime.Year == nam
                                         && nv.CheckTime.Day == Ngay
                                         && nv.UserID == localID
                                         && nv.CheckType == (int)DTO.CheckDTO.CheckTypeE.check_in
                                   select nv
                           ).Count();
              
            }

            return Earlytime;

        }
        // 
        public static CheckDTO GetJobByday(int Ngay,int thang,int nam,Int32 localID)
        {
            CheckDTO listNhanViens = null;

            using (Moved.Moved_DB dc = new Moved_DB(_connString))
            {
                var nhanViens = (from nv in dc.AL_CheckInOut
                                 join user in dc.AL_UserInfo on nv.UserID equals user.UserID 
                                 where nv.CheckTime.Month == thang
                                       && nv.CheckTime.Year == nam
                                       && nv.CheckTime.Day == Ngay
                                       && nv.UserID == localID
                                       && nv.CheckType == (int)DTO.CheckDTO.CheckTypeE.check_in
                                 select new
                                 {
                                     id = nv.UserID,
                                     machine = nv.MachineID,
                                     checktime = nv.CheckTime,
                                     checktype = nv.CheckType,
                                     verify = nv.VerifyMode,
                                     workcode = nv.WorkCode
                                 }
                                         ).Distinct().OrderBy(temp => temp.id).ThenBy(temp=>temp.checktime);

                //var nhanVienObj = nhanViens.First();
                foreach (var nhanVienObj in nhanViens)
                {
                    listNhanViens = new CheckDTO();
                    listNhanViens.UserID = nhanVienObj.id;
                    listNhanViens.MachineID = (int)nhanVienObj.machine;
                    listNhanViens.ChecType = (int)nhanVienObj.checktype;
                    listNhanViens.CheckTime = nhanVienObj.checktime;
                    listNhanViens.VerifyMode = (int)nhanVienObj.verify;
                    break;
                }
            }
            return listNhanViens;
        }
        public static CheckDTO GetJobBydayout(int Ngay, int thang, int nam, Int32 localID)
        {
            CheckDTO listNhanViens = null;

            using (Moved.Moved_DB dc = new Moved_DB(_connString))
            {
                var nhanViens = (from nv in dc.AL_CheckInOut
                                 join user in dc.AL_UserInfo on nv.UserID equals user.UserID
                                 where nv.CheckTime.Month == thang
                                       && nv.CheckTime.Year == nam
                                       && nv.CheckTime.Day == Ngay
                                       && nv.UserID == localID
                                       && nv.CheckType == (int)DTO.CheckDTO.CheckTypeE.check_out
                                 select new
                                 {
                                     id = nv.UserID,
                                     machine = nv.MachineID,
                                     checktime = nv.CheckTime,
                                     checktype = nv.CheckType,
                                     verify = nv.VerifyMode,
                                     workcode = nv.WorkCode
                                 }
                                         ).Distinct().OrderBy(temp => temp.id).ThenByDescending(temp => temp.checktime);

                //var nhanVienObj = nhanViens.First();
                foreach (var nhanVienObj in nhanViens)
                {
                    listNhanViens = new CheckDTO();
                    listNhanViens.UserID = nhanVienObj.id;
                    listNhanViens.MachineID = (int)nhanVienObj.machine;
                    listNhanViens.ChecType = (int)nhanVienObj.checktype;
                    listNhanViens.CheckTime = nhanVienObj.checktime;
                    listNhanViens.VerifyMode = (int)nhanVienObj.verify;
                    break;
                }
            }
            return listNhanViens;
        }

        public static CheckDTO GetTimeMealOut(int Ngay, int thang, int nam, Int32 localID, Double LimitedTimeMeal, Double TimeEat)
        {
            CheckDTO listNhanViens = null;
            Double giohanantrua = LimitedTimeMeal;
            Double Thoigianan = TimeEat;
            using (Moved.Moved_DB dc = new Moved_DB(_connString))
            {
                var nhanViens = (from nv in dc.AL_CheckInOut
                                 join user in dc.AL_UserInfo on nv.UserID equals user.UserID
                                 where nv.CheckTime.Month == thang
                                       && nv.CheckTime.Year == nam
                                       && nv.CheckTime.Day == Ngay
                                       && nv.UserID == localID
                                       && nv.CheckType == (int)DTO.CheckDTO.CheckTypeE.check_out
                                       && nv.CheckTime.TimeOfDay.Hours <= giohanantrua
                                       && nv.CheckTime.TimeOfDay.Hours >= Thoigianan
                                 select new
                                 {
                                     id = nv.UserID,
                                     machine = nv.MachineID,
                                     checktime = nv.CheckTime,
                                     checktype = nv.CheckType,
                                     verify = nv.VerifyMode,
                                     workcode = nv.WorkCode
                                 }
                                         ).Distinct().OrderBy(temp => temp.id).ThenBy(temp => temp.checktime);
                //var nhanVienObj = nhanViens.First();
                foreach (var nhanVienObj in nhanViens)
                {
                    listNhanViens = new CheckDTO();
                    listNhanViens.UserID = nhanVienObj.id;
                    listNhanViens.MachineID = (int)nhanVienObj.machine;
                    listNhanViens.ChecType = (int)nhanVienObj.checktype;
                    listNhanViens.CheckTime = nhanVienObj.checktime;
                    listNhanViens.VerifyMode = (int)nhanVienObj.verify;
                    break;
                }
            }
            return listNhanViens;
        }
        public static DTO.CheckDTO GetTimeMeal(int Ngay, int thang, int nam, Int32 localID, double MealTimeFromIn, double MealTimeToIn)
        {
            Int32 day = Ngay;
            Int32 month = thang;
            Int32 year = nam;
            Int32 UserID = localID;
            Double GioiHanVao = MealTimeFromIn;
            Double GioiHanRa = MealTimeToIn;
            DTO.CheckDTO MealTime = null;
            using (Moved.Moved_DB dc = new Moved_DB(_connString))
            {
                var objTimeMeal = (from sh in dc.AL_CheckInOut
                                   where sh.CheckTime.Day == day
                                    && sh.CheckTime.Month == month
                                    && sh.CheckTime.Year == year
                                    && sh.CheckType == (int)CheckDTO.CheckTypeE.check_in
                                    && sh.UserID == localID
                                    && sh.CheckTime.TimeOfDay.Hours >= GioiHanVao
                                    && sh.CheckTime.TimeOfDay.Hours <= GioiHanRa
                                   select new
                                   {
                                       GioAnTrua = sh.CheckTime
                                   }
                                     ).Distinct();
                foreach (var sh in objTimeMeal)
                {
                    MealTime = new CheckDTO();
                    MealTime.CheckTime = sh.GioAnTrua;
                }
                return MealTime;
            }

        }

    }
}
